import handler.commandprocessor.CommandEncoder;
import handler.commandprocessor.ReceiveDecoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import message.Receive;
import message.ReceiveType;
import message.command.JoinGroupCommand;
import message.command.LoginCommand;
import message.command.RegisterCommand;
import message.command.SendToGroupCommand;
import message.receive.*;

import java.util.EnumMap;

public class Client2
{
    public static void main(String[] args) throws InterruptedException
    {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(new NioEventLoopGroup(1));
        bootstrap.channel(NioSocketChannel.class);
        final EnumMap<ReceiveType, Class<? extends Receive>> enumMap = new EnumMap<ReceiveType, Class<? extends Receive>>(ReceiveType.class);
        enumMap.put(ReceiveType.LOGIN, LoginReceive.class);
        enumMap.put(ReceiveType.REGISTER, RegisterReceive.class);
        enumMap.put(ReceiveType.MSG_FROM_CLIENT, MsgFromClientReceive.class);
        enumMap.put(ReceiveType.CREATE_GROUP, CreateGroupReceive.class);
        enumMap.put(ReceiveType.JOIN_GROUP,JoinGroupReceive.class);
        enumMap.put(ReceiveType.MSG_FROM_GROUP, MsgFromGroupReceive.class);
        bootstrap.handler(new ChannelInitializer<SocketChannel>()
        {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception
            {
                ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024, 0, 4, 0, 4));
                ch.pipeline().addLast(new ReceiveDecoder(enumMap));
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter()
                {
                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
                    {
                        if (msg instanceof LoginReceive)
                        {
                            String codeMsg = ((LoginReceive) msg).getCodeMsg();
                            System.out.println("登录响应:" + codeMsg);
                        }
                        else if (msg instanceof RegisterReceive)
                        {
                            String codeMsg = ((RegisterReceive) msg).getCodeMsg();
                            System.out.println("注册响应：" + codeMsg);
                        }
                        else if (msg instanceof MsgFromClientReceive)
                        {
                            String clientName = ((MsgFromClientReceive) msg).getClientName();
                            String msg1       = ((MsgFromClientReceive) msg).getMsg();
                            System.out.println("收到来自客户端:" + clientName + " 的消息：" + msg1);
                        }
                        else if (msg instanceof CreateGroupReceive)
                        {
                            String codeMsg = ((CreateGroupReceive) msg).getCodeMsg();
                            System.out.println("群聊创建响应:" + codeMsg);
                        }
                        else if(msg instanceof  JoinGroupReceive){
                            String codeMsg = ((JoinGroupReceive) msg).getCodeMsg();
                            System.out.println("加入群聊响应:"+codeMsg);
                        }
                        else if (msg instanceof MsgFromGroupReceive)
                        {
                            String clientName = ((MsgFromGroupReceive) msg).getClientName();
                            String groupName  = ((MsgFromGroupReceive) msg).getGroupName();
                            String msg1       = ((MsgFromGroupReceive) msg).getMsg();
                            System.out.println("收到群聊{" + groupName + "}中{" + clientName + "}的消息:" + msg1);
                        }
                        else
                        {
                            System.out.println("当前还未识别的响应的内容");
                        }
                    }
                });
                ch.pipeline().addLast(new CommandEncoder());
            }
        });
        ChannelFuture connect = bootstrap.connect("127.0.0.1", 8888);
        connect.sync();
        Channel channel    = connect.channel();
        String  clientName = "深入浅出学Netty读者：client2";
        System.out.println("准备登录:" + clientName);
//        RegisterCommand registerCommand = new RegisterCommand();
//        registerCommand.setRegisterName(clientName);
//        channel.writeAndFlush(registerCommand);
        LoginCommand loginCommand = new LoginCommand();
        loginCommand.setLoginName(clientName);
        channel.writeAndFlush(loginCommand);
//        JoinGroupCommand createGroupCommand = new JoinGroupCommand();
//        createGroupCommand.setGroupName("Netty学习小组");
//        channel.writeAndFlush(createGroupCommand);
//        SendToGroupCommand command = new SendToGroupCommand();
//        command.setGroupName("Netty学习小组");
//        command.setMsg("你好");
//        channel.writeAndFlush(command);
        channel.closeFuture().sync();
    }
}
